<?php

/**
 * floor.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-14 18:45
 * @modified 2018-07-14 18:45
 */
class ControllerExtensionModuleFloor extends Controller
{
    private $error = array();

    public function index()
    {
        $this->load->language('extension/module/floor');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('setting/module');
        $this->document->addScript('view/javascript/spectrum/spectrum.js');
        $this->document->addStyle('view/javascript/spectrum/spectrum.css');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            if (!isset($this->request->get['module_id'])) {
                $this->model_setting_module->addModule('floor', $this->request->post);
            } else {
                $this->model_setting_module->editModule($this->request->get['module_id'], $this->request->post);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        } else {
            $data['error_name'] = '';
        }


        $breads = new Breads();
        $breads->add(trans('text_home'), uriLink('common/dashboard', 'user_token=' . $this->session->data['user_token']));
        $breads->add(trans('text_extension'), uriLink('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module'));
        if (isset($this->request->get['module_id'])) {
            $breads->add(trans('heading_title'), uriLink('extension/module/floor', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id']));
            $data['action'] = uriLink('extension/module/floor', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id']);
        } else {
            $breads->add(trans('heading_title'), uriLink('extension/module/floor', 'user_token=' . $this->session->data['user_token']));
            $data['action'] = uriLink('extension/module/floor', 'user_token=' . $this->session->data['user_token']);
        }
        $data['breadcrumbs'] = $breads->get();

        if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $module_info = $this->model_setting_module->getModule($this->request->get['module_id']);
        }

        if (isset($this->request->post['name'])) {
            $data['name'] = $this->request->post['name'];
        } elseif (!empty($module_info)) {
            $data['name'] = $module_info['name'];
        } else {
            $data['name'] = '';
        }

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (!empty($module_info)) {
            $data['image'] = $module_info['image'];
        } else {
            $data['image'] = '';
        }

        if (isset($this->request->post['color'])) {
            $data['color'] = $this->request->post['color'];
        } elseif (!empty($module_info)) {
            $data['color'] = $module_info['color'];
        } else {
            $data['color'] = '';
        }

        if (isset($this->request->post['status'])) {
            $data['status'] = $this->request->post['status'];
        } elseif (!empty($module_info) && isset($module_info['status'])) {
            $data['status'] = $module_info['status'];
        } else {
            $data['status'] = '';
        }

        if (isset($this->request->post['title_recommend'])) {
            $data['title_recommend'] = $this->request->post['title_recommend'];
        } elseif (!empty($module_info)) {
            $data['title_recommend'] = $module_info['title_recommend'];
        } else {
            $data['title_recommend'] = array();
        }

        if (isset($this->request->post['image_recommend'])) {
            $data['image_recommend'] = $this->request->post['image_recommend'];
        } elseif (!empty($module_info)) {
            $data['image_recommend'] = $module_info['image_recommend'];
        } else {
            $data['image_recommend'] = array();
        }

        if (isset($this->request->post['banners'])) {
            $banners = $this->request->post['banners'];
        } elseif (!empty($module_info)) {
            $banners = $module_info['banners'];
        } else {
            $banners = array();
        }

        $data['banners'] = array();
        if ($banners) {
            foreach ($banners as $banner) {
                $banner_image = $banner['image'];
                $banner_thumb = resizeImage($banner_image);
                $data['banners'][] = array(
                    'image' => $banner_image,
                    'thumb' => $banner_thumb,
                    'title' => $banner['title'],
                    'href' => $banner['href']
                );
            }
        }


        if (isset($this->request->post['products'])) {
            $products = $this->request->post['products'];
        } elseif (!empty($module_info)) {
            $products = $module_info['products'];
        } else {
            $products = array();
        }

        $data['products'] = array();
        $this->load->model('catalog/product');
        if ($products) {
            foreach ($products as $product_id) {
                $product_info = $this->model_catalog_product->getProduct($product_id);

                if ($product_info) {
                    $data['products'][] = array(
                        'product_id' => $product_info['product_id'],
                        'name' => $product_info['name']
                    );
                }
            }
        }

        $data['user_token'] = $this->session->data['user_token'];

        $data['thumb'] = resizeImage($data['image']);
        $data['placeholder'] = resizeImage('no_image.png');

        $this->load->model('localisation/language');
        $data['languages'] = $this->model_localisation_language->getLanguages();

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $this->response->setOutput($this->load->view('extension/module/floor', $data));
    }

    protected function validate()
    {
        if (!$this->user->hasPermission('modify', 'extension/module/floor')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
            $this->error['name'] = $this->language->get('error_name');
        }

        return !$this->error;
    }
}